package com.ywc.tools.contentprovide;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.ywc.tool.SQLiteUtil;

public class YwcProvide extends ContentProvider{

    private String keyString[]={"name","age"};
    private String keyType[]={"text","integer"};
    private String personl="personl";
    private SQLiteUtil personlSQLite;

    private static final UriMatcher MATCHER=new UriMatcher(UriMatcher.NO_MATCH);
    private static final int PERSON=1;
    private static final int TEACHER=2;
    static {
        MATCHER.addURI("cn.YwcSillyWhite.com", "person", PERSON);
        MATCHER.addURI("cn.YwcSillyWhite.com", "teacher", TEACHER);
    }



    @Override
    public boolean onCreate() {
        personlSQLite = new SQLiteUtil(this.getContext(), personl, keyString, keyType);
        return false;
    }

    /**
     *
     * @param uri 查询某个应用程序下的某一张表
     * @param projection 指定查询的列名
     * @param selection  指定where的约束条件
     * @param selectionArgs 为where中的占位符提供具体值
     * @param sortOrder 指定查询结果的排序方式
     * @return
     */
    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        switch(MATCHER.match(uri))
        {
            case PERSON:
                Cursor query = personlSQLite.query(projection, selection, selectionArgs, null, null, sortOrder);
                personlSQLite.close();
                return query;
            default:
                return null;
        }
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        switch(MATCHER.match(uri))
        {
            case PERSON:
                return "content://cn.YwcSillyWhite.com/person";
            default:
                return null;
        }
    }


    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
        switch(MATCHER.match(uri))
        {
            case PERSON:
                Long insert = personlSQLite.insert(values);
                personlSQLite.close();
                return ContentUris.withAppendedId(uri, insert);
            default:
                return null;
        }
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
        switch(MATCHER.match(uri))
        {
            case PERSON:
                int deleter = personlSQLite.deleter(selection, selectionArgs);
                personlSQLite.close();
                return deleter;
            default:
                return 0;
        }
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        switch(MATCHER.match(uri))
        {
            case PERSON:
                int update = personlSQLite.update(values, selection, selectionArgs);
                personlSQLite.close();
                return update;
            default:
                return 0;
        }
    }

}
